package com.gpzx.zx.module.home.frame.view

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gpzx.zx.R
import com.gpzx.zx.module.home.frame.model.QKLHomeNewsInfo
import com.gpzx.zx.module.home.frame.model.QKLNewsInfo
import com.gpzx.zx.module.home.frame.model.QKLTagInfo
import com.gpzx.zx.module.home.ui.activity.AuthorActivity
import com.gpzx.zx.module.home.ui.activity.HomeNewsListActivity
import com.gpzx.zx.module.home.ui.activity.InfoWebViewActivity
import com.gpzx.zx.module.home.ui.adapter.GpOrgTypeAdapter
import com.gpzx.zx.module.home.ui.adapter.HomeNewsAdapter
import com.gpzx.zx.module.home.ui.fragment.HomeFragment
import com.gpzx.zx.utlis.QklDbUtlis
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.lib_base.adapter.KtDataBindingAdapter
import com.wu.common.base.HomeTopBean
import com.wu.common.base.adapter.MoveTopAdapter
import com.wu.common.utils.AlertUtil
import com.youth.banner.listener.OnBannerListener
import com.youth.banner.util.BannerUtils
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout


/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/6/7
 *
 * 用途:
 */


class HomeView : MvpView, View.OnClickListener {

    var mFragment: HomeFragment

    constructor(mFragment: HomeFragment) {
        this.mFragment = mFragment
    }

    fun initView() {
//        mFragment.binding.appbarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
//            if (Math.abs(verticalOffset) >= appBarLayout.totalScrollRange) {
//                if (!mFragment.isExpend) {
//                    StatusBarUtil.setDarkMode(mFragment.activity)
//                    mFragment.isExpend = !mFragment.isExpend
//                    mFragment.binding.toolbar.visibility = View.VISIBLE
//                }
//            } else {
//                if (mFragment.isExpend) {
//                    StatusBarUtil.setDarkMode(mFragment.activity)
//                    mFragment.isExpend = !mFragment.isExpend
//                    mFragment.binding.toolbar.visibility = View.GONE
//                }
//            }
//        })
        initTop()
        initOrgType()
        initNews()
        initTag()
    }

    private fun initTag() {
        var mInflater = LayoutInflater.from(mFragment.getActivity());

        var db2 = QklDbUtlis(mFragment.activity as Context)
        var list = db2.tags
        mFragment.binding.idFlowlayout.setAdapter(object : TagAdapter<QKLTagInfo?>(list) {
            override fun getView(parent: FlowLayout?, position: Int, s: QKLTagInfo?): View? {
                val tv = mInflater.inflate(R.layout.tv,
                        parent, false) as TextView
                tv.text = s!!.tagName
                return tv
            }
        })
        mFragment.binding.idFlowlayout.setOnTagClickListener(object : TagFlowLayout.OnTagClickListener {

            override fun onTagClick(view: View?, position: Int, parent: FlowLayout?): Boolean {
                HomeNewsListActivity.newInstance(mFragment.activity as Context, list.get(position).tagName.toString())
                return true
            }

        })
    }

    private fun initNews() {
        var db2 = QklDbUtlis(mFragment.activity as Context)
        var newTypes: MutableList<String> = ArrayList()
        var homeNews: MutableList<QKLHomeNewsInfo> = ArrayList()
        var gpNews = db2.getNewsByType("行情")


        gpNews.forEach {
            if (!newTypes.contains(it.news_type)) {
                newTypes.add(it.news_type!!)
                var homegroupInfo = QKLHomeNewsInfo()
                homegroupInfo.title = "行情"
                homegroupInfo.type = 1
                homeNews.add(homegroupInfo)
                var cont = 0;
                gpNews.forEach {
                    var homeNewInfo = QKLHomeNewsInfo()
                    homeNewInfo.type = 2
                    homeNewInfo.title = homegroupInfo.title
                    homeNewInfo.newInfo = it
                    if (cont < 4) {
                        cont += 1
                        homeNews.add(homeNewInfo)
                    }
                }
            }
        }
        var db = QklDbUtlis(mFragment.activity as Context)
        var gpNews2 = db.getNewsByType("新闻")
        gpNews2.forEach {
            if (!newTypes.contains(it.news_type)) {
                newTypes.add(it.news_type!!)
                var homegroupInfo = QKLHomeNewsInfo()
                homegroupInfo.title = "新闻"
                homegroupInfo.type = 1
                homeNews.add(homegroupInfo)
                var cont = 0;
                gpNews.forEach {
                    var homeNewInfo = QKLHomeNewsInfo()
                    homeNewInfo.type = 2
                    homeNewInfo.title = "投研"
                    homeNewInfo.newInfo = it
                    if (cont < 4) {
                        cont += 1
                        homeNews.add(homeNewInfo)
                    }
                }
            }
        }
        var db3 = QklDbUtlis(mFragment.activity as Context)
        var gpNews3 = db3.getNewsByType("政策")
        gpNews3.forEach {
            if (!newTypes.contains(it.news_type)) {
                newTypes.add(it.news_type!!)
                var homegroupInfo = QKLHomeNewsInfo()
                homegroupInfo.title = "政策"
                homegroupInfo.type = 1
                homeNews.add(homegroupInfo)
                var cont = 0;
                gpNews.forEach {
                    var homeNewInfo = QKLHomeNewsInfo()
                    homeNewInfo.type = 2
                    homeNewInfo.title = "深度"
                    homeNewInfo.newInfo = it
                    if (cont < 4) {
                        cont += 1
                        homeNews.add(homeNewInfo)
                    }
                }
            }
        }


        var homeAdapter = HomeNewsAdapter(mFragment.activity as Context)
        mFragment.binding.rvContent.layoutManager = LinearLayoutManager(mFragment.activity)
        mFragment.binding.rvContent.adapter = homeAdapter
        homeAdapter.addItems(homeNews)
        homeAdapter.setOnViewClickListener(object : HomeNewsAdapter.OnAdapterViewClickListener<QKLHomeNewsInfo> {
            override fun onViewClick(v: Int, program: QKLHomeNewsInfo?) {
                if (v == 1) {
                    HomeNewsListActivity.newInstance(mFragment.activity as Context, program!!.title!!)
                } else {
                    InfoWebViewActivity().newInstance(mFragment.activity as Context, "qkl_news", program?.newInfo!!.id, program?.newInfo!!.news_title)
                }
            }
        })

    }

    private fun initOrgType() {

        var db2 = QklDbUtlis(mFragment.activity as Context)
        var authorsss = db2?.get7AuthorNews()
        var homeNews: MutableList<QKLNewsInfo> = ArrayList()
        authorsss.forEach {
            var homegroupInfo = QKLNewsInfo()
            if (homeNews.size < 6 && !TextUtils.isEmpty(it.news_icon)) {
                homegroupInfo.news_author_name = it.news_author_name
                homegroupInfo.news_author_icon = it.news_author_icon!!.split("?").get(0)
                homegroupInfo.news_author_id = it.news_author_id
                homeNews.add(homegroupInfo)

            }
        }
        mFragment.binding.rvOrg.layoutManager = GridLayoutManager(mFragment.activity, 3) as RecyclerView.LayoutManager?

        var mAdapter = GpOrgTypeAdapter(mFragment.activity as Context, R.layout.item_org_type)
        mFragment.binding.rvOrg.adapter = mAdapter
        mAdapter.addItems(homeNews)
        mAdapter.setOnViewClickListener(object : KtDataBindingAdapter.OnAdapterViewClickListener<QKLNewsInfo> {
            override fun onViewClick(v: View?, program: QKLNewsInfo?) {
                AuthorActivity.newInstance(mFragment.activity as Context, program!!.news_author_id + "")
            }
        })
    }

    private fun initTop() {

        var db2 = QklDbUtlis(mFragment.activity as Context)
        var topNews = db2?.getNewsByType("hot")

        var topInfos: MutableList<HomeTopBean> = ArrayList()

        topNews.forEach {
            var topQh = HomeTopBean()

            if (topInfos.size < 6 && !TextUtils.isEmpty(it.news_icon) && it.news_icon!!.contains("http")) {
                topQh.title = it.news_title
                if (!it!!.news_icon!!.contains("https://cdn.55coin.com/article/img/0/15955975863731379.png")){
                    if (it!!.news_icon!!.contains(":")) {
                        topQh.path = it.news_icon!!.split("?").get(0)
                        topQh.url = it.id
                        topInfos.add(topQh)
                    }
                }


            }
        }


        mFragment.binding.bannerMovies.setAdapter(MoveTopAdapter(topInfos))
        mFragment.binding.bannerMovies.setIndicator(mFragment.binding.indicator, false)
        mFragment.binding.bannerMovies.setIndicatorSelectedWidth(BannerUtils.dp2px(15f).toInt())

        mFragment.binding.bannerMovies.setOnBannerListener(object : OnBannerListener<HomeTopBean> {
            override fun OnBannerClick(data: HomeTopBean?, position: Int) {
                openWeb(data)
            }
        })


    }


    private fun openWeb(data: HomeTopBean?) {
        InfoWebViewActivity().newInstance(mFragment?.activity as Context, "qkl_gp_top", data?.url, data?.title)
    }

    fun showMessage(message: String?) {
        if (mFragment == null || TextUtils.isEmpty(message)) return
        AlertUtil.showDeftToast(mFragment!!.activity, message)
    }

    override fun onClick(v: View?) {
        when (v!!.id) {


        }
    }


}